package com.zhxy.controller;

import com.github.pagehelper.PageInfo;
import com.zhxy.bean.User;
import com.zhxy.result.R;
import com.zhxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取当前登录用户
     * @param authentication
     * @return
     */
    @GetMapping("/api/user/info")
    public R getUser(Authentication authentication){

        User user = (User) authentication.getPrincipal();
        return R.OK(user);
    }

    /**
     * 用户免登录
     * @return
     */
    @GetMapping("/api/login/free")
    public R freeLogin(){
        return R.OK();
    }

    /**
     * 用户列表分页查询
     * @return
     */
    @GetMapping("/api/user/list")
    public R userPage(@RequestParam Integer current){
        if (current==null){
            current=1;
        }
        PageInfo<User> userList=userService.getUserByPage(current);
        return R.OK(userList);
    }
}
